<?php

class filter {
	private $tagallow;
	private $tagthru;
	private $attrallow;
	function __construct() {
		$this->tagallow = array('p','a','img','em','strong','pre','br','i','b','h2','h3');
		$this->tagthru = array('div','span');
		$this->attrallow = array('src','href');
		$this->attrkill = array('class'=>'ad','class'=>'ad-ad','class'=>'path');
	}


	function go($dom) {
		$tag = $dom->tag;
			
		if ($tag == 'text') {
			return $dom->innertext;
		}
		
		$l = 0;
		if (in_array($tag, $this->tagthru)) $l = 1;
		if (in_array($tag, $this->tagallow)) $l = 2;
		
		if (!$l) return '';
		$out='';
		
		$attrs = '';
		$a = $dom->attr;
		foreach ($a as $n=>$v) {
			if (isset($this->attrkill[$n]) && ($this->attrkill[$n] == $v) ) return '';
			if (!in_array($n, $this->attrallow)) continue;
			$attrs.= ' '. $n.'="'.$v.'"';
		}
		
		if ($l == 2) $out = '<'.$tag.$attrs.'>';
		
		$b = $dom->nodes;
		
		if (count($b)) {
			foreach ($b as $el) {
				$out.=$this->go($el);
			}
		} else {
			$out .= $dom->innertext;
		}
		
		if ($l == 2) $out .= '</'.$tag.'>';
		return $out;
	}
}
?>